﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Data.SqlClient;
using Twi.DataAccess.DBUtility;
using System.ServiceModel;
using System.Data.Common;
using System.IO;
using System.ServiceModel.Channels;

namespace Twi.COMMON.WCF
{
    public class ProcessRequest1
    {
        public static XElement Process(Action<ResponseMessageInfo, SqlCommand> processDelegate)
        {
            var resInfo = new ResponseMessageInfo();
            string version = HttpHelper.QueryString("version");  //版本号/Http请求已关闭/其他请求
            if (version != resInfo.ClientVersion && version != "Http请求已关闭" && version != "其他请求" && version != "服务端自请求")
            {
                resInfo.Status = ResponseMessageInfoStatus.ClientVersionException;
                resInfo.Exception = "您当前使用版本不是最新版本，请升级至最新版本！";
                return resInfo.DataRoot;
            }



            SqlCommand cmd = new SqlCommand();
            cmd.CommandTimeout = 300;
            try
            {
                processDelegate(resInfo, cmd);

            }
            catch (RequestException rex)
            {
                resInfo.Status = rex.ResponseMessageInfoStatus;
                resInfo.Exception = rex.Message;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null && ex.InnerException is RequestException)
                {
                    var rex = ex.InnerException as RequestException;
                    resInfo.Status = rex.ResponseMessageInfoStatus;
                    resInfo.Exception = rex.Message;
                }
                else
                {

                    //如果未指定错误类型，就默认指定
                    if (resInfo.Status == ResponseMessageInfoStatus.ServerResultSuccess)
                    {
                        resInfo.Status = ResponseMessageInfoStatus.ServerException;
                        resInfo.Exception = ex.GetFullMessage();
                    }
                    else
                    {
                        resInfo.Exception = ex.Message;
                    }
                }

            }
            finally
            {
                if (cmd != null)
                {
                    if (cmd.Connection != null && cmd.Connection.State == System.Data.ConnectionState.Open)
                    {
                        cmd.Connection.Close();
                        cmd.Connection.Dispose();
                    }
                    cmd.Dispose();
                }
            }
            return resInfo.DataRoot;
        }
        public static XElement Process(Action<ResponseMessageInfo> processDelegate)
        {
            var resInfo = new ResponseMessageInfo();
            string version = HttpHelper.QueryString("version");  //版本号/Http请求已关闭/其他请求
            if (version != resInfo.ClientVersion && version != "Http请求已关闭" && version != "其他请求" && version != "服务端自请求")
            {
                resInfo.Status = ResponseMessageInfoStatus.ClientVersionException;
                resInfo.Exception = "您当前使用版本不是最新版本，请升级至最新版本！";
                resInfo.Message = "您当前使用版本不是最新版本，请升级至最新版本！";
                return resInfo.DataRoot;
            }

            try
            {
                processDelegate(resInfo);
            }
            catch (RequestException rex)
            {
                resInfo.Status = rex.ResponseMessageInfoStatus;
                resInfo.Exception = rex.Message;
                resInfo.Message = rex.Message;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null && ex.InnerException is RequestException)
                {
                    var rex = ex.InnerException as RequestException;
                    resInfo.Status = rex.ResponseMessageInfoStatus;
                    resInfo.Exception = rex.Message;
                    resInfo.Message = rex.Message;
                }
                else
                {
                    //如果未指定错误类型，就默认指定
                    if (resInfo.Status == ResponseMessageInfoStatus.ServerResultSuccess)
                    {
                        resInfo.Status = ResponseMessageInfoStatus.ServerException;
                        resInfo.Exception = ex.GetFullMessage();
                        resInfo.Message = ex.Message;
                    }
                    else
                    {
                        resInfo.Exception = ex.Message;
                        resInfo.Message = ex.Message;
                    }
                }
            }
            return resInfo.DataRoot;
        }

        public static ResponseMessageInfo Process(Action<ResponseMessageInfo, SqlTransaction> processDelegate, SqlConnection conn)
        {
            var resInfo = new ResponseMessageInfo();
            SqlTransaction txn = conn.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);
            try
            {
                processDelegate(resInfo, txn);
            }
            catch (Exception ex)
            {
                txn.Rollback();

                //如果未指定错误类型，就默认指定
                if (resInfo.Status == ResponseMessageInfoStatus.ServerResultSuccess)
                {
                    resInfo.Status = ResponseMessageInfoStatus.ServerException;
                    resInfo.Exception = ex.GetFullMessage();
                }
                else
                {
                    resInfo.Exception = ex.Message;
                }
            }
            finally
            {
                if (conn.State == System.Data.ConnectionState.Open)
                {
                    //有可能该事物已经回滚不能提交
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "select @@trancount";
                    cmd.Transaction = txn;

                    if ((int)cmd.ExecuteScalar() > 0)
                    {
                        txn.Commit();
                    }
                    txn.Dispose();
                }
            }
            return resInfo;
        }
    }
}
